<?php

/*
 * This file is part of the Geek-Zoo Projects.
 *
 * @copyright (c) 2010 Geek-Zoo Projects More info http://www.geek-zoo.com
 * @license http://opensource.org/licenses/gpl-2.0.php The GNU General Public License
 * @author quqiang <quqiang@geek-zoo.com>
 *
 */

class item_model extends Model
{
	function get_item_ralation($item_id, $limit = 5, $type = 0)
	{
		$item_cate = $this->db->getCol("SELECT category_id FROM item_ralation WHERE item_id=$item_id");
		
		if (empty($item_cate)) {
			return $data = array();
		} else {
			$item_cate = implode(',', $item_cate);
			$data = $this->db->getAll("SELECT DISTINCT b.* FROM item_ralation as a LEFT JOIN item as b ON a.item_id=b.id WHERE a.category_id IN($item_cate) AND b.type='$type' AND b.status !='0' AND b.status !='3' AND b.id!='$item_id' limit $limit ");
			if ($type == 1) {
				foreach ($data as $key => $value) {
					$data[$key]['data'] = unserialize($value['data']);
				}
			}
			return $data;
		}
	}
	
	function get_cate_item($cate_id, $pager, $type = 0, $recommend = 0)
	{
		$limit = $pager->setPage()->getLimit();
		$and = " WHERE  b.status !='0' AND b.status !='3'  ";
		if (!empty($cate_id)) {
			$cate_id = implode(',', $cate_id);
			$and .= " AND a.category_id IN($cate_id) ";
		}
		if ($type == 1) {
			$and .= " AND b.type='1' ";
		}
		if ($type == 2) {
			$and .= " AND b.type='0' ";
		}
		if ($recommend) {
			$and .= " AND b.is_recommend='$recommend' ";
		}
		$data = $this->db->getAll("SELECT DISTINCT b.* FROM item_ralation as a LEFT JOIN item as b ON a.item_id=b.id $and $limit");
		$count = $this->db->getOne("SELECT count(DISTINCT a.item_id) FROM item_ralation as a LEFT JOIN item as b ON a.item_id=b.id $and");
        $pager->generate($count);
        return array(
            'data' => $data,
            'pager' => $pager
        );
	}
}

?>